简介

iterator标签用来遍历一个数组、Collection或一个Map,并把这个可遍历对象的每一个元素一次压入和弹出。

例子

为了搞清楚OGNL中s:iteraor标签的使用方法,这里举了一个例子:

  • User.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    package com.glemontree.struts;
    public class User {
    private String userName;
    private String password;
    private Integer age;
    public String getUserName() {
    return userName;
    }
    public void setUserName(String userName) {
    this.userName = userName;
    }
    public String getPassword() {
    return password;
    }
    public void setPassword(String password) {
    this.password = password;
    }
    public Integer getAge() {
    return age;
    }
    public void setAge(Integer age) {
    this.age = age;
    }
    public User() {
    // TODO Auto-generated constructor stub
    }
    public User(String userName, String password, Integer age) {
    super();
    this.userName = userName;
    this.password = password;
    this.age = age;
    }
    }
  • IteratorTestAction.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    package com.glemontree.struts;
    import java.util.ArrayList;
    import java.util.List;
    public class IteratorTestAction {
    User user;
    public User getUser() {
    return user;
    }
    public void setUser(User user) {
    this.user = user;
    }
    public String execute() {
    user = new User();
    user.setUserName("aaa");
    user.setPassword("123");
    user.setAge(23);
    return "success";
    }
    }

    这是一个Action类,类中含有一个User对象。

  • index.jsp

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
    <title>Insert title here</title>
    </head>
    <body>
    <a href="IteratorTestAction.action"> Iterator Test </a>
    </body>
    </html>
  • struts.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
    <struts>
    <!-- 打开静态方法的限制 -->
    <package name="default" namespace="/" extends="struts-default">
    <action name="IteratorTestAction" class="com.glemontree.struts.IteratorTestAction">
    <result>/details.jsp</result>
    </action>
    </package>
    </struts>
  • details.jsp

    • 测试一

      1
      2
      OGNL:<s:property value="user.age"/><br>
      EL表达式:${user.age }

      因为IteratorTestAction.java类中含有一个User对象,因此值栈中也含有user对象,因此既可以通过OGNL,也可以通过EL表达式获取到user.age属性。

      该测试结果为:

      1
      2
      3
      测试一:
      OGNL:23
      EL表达式:23
    • 测试二

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      <%
      List<User> users = new ArrayList<User>();
      users.add(new User("aaa","123",12));
      users.add(new User("bbb","456",13));
      users.add(new User("ccc","789",14));
      users.add(new User("ddd","130",15));
      users.add(new User("eee","458",16));
      request.setAttribute("users", users);
      %>
      <s:iterator value="#request.users">
      <s:debug></s:debug>
      ${userName }<br>
      <s:property value="userName"/>
      </s:iterator>

      首先定义了一个List类型的对象users,并且王users里面添加了几个值,紧接着使用<s:iterator>标签对List进行操作,<s:iterator>的value属性表示集合。

      这里为了看清每次遍历过程中值栈的状态,在<s:iterator>中加入了<s:debug>标签,运行结果如下:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      [Debug]
      aaa
      [Debug]
      bbb
      [Debug]
      ccc
      [Debug]
      ddd
      [Debug]
      eee

      点击其中一个[Debug]标签,结果如下:

      可见正如上面所说,在每次迭代过程中都会将当前的元素压入值栈。